﻿package sedie 
{
	import flash.display.DisplayObject;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import game.BetPlace;
	import game.Chip;
	import game.ChipController;
	import game.events.EventData;
	import sedie.exchangeddata.BetInfo;
	import common.Utility;
	import game.GlobalVariables;
	/**
	 * ...
	 * @author Doan Nguyen Thuy Nha Truc (Bamboo)	
	 */
	public class BetController extends game.BetController
	{
		
		public  var Sd00, Sd04, Sd01, Sd03, SdOd, SdEv:SimpleButton;
		
		public  var Sd00Bet, Sd04Bet, Sd01Bet, Sd03Bet, SdOddBet, SdEvenBet:BetPlace;
		
		
		public var betInfo:sedie.exchangeddata.BetInfo;
		public var saveBetInfo:sedie.exchangeddata.BetInfo;
		public var preBetInfo:sedie.exchangeddata.BetInfo;
		
		public var rateArr:Array;
		
		public var posBetted:Object;
		public function BetController() 
		{
			posBetted = new Object();
			betInfo = new BetInfo();
			saveBetInfo = new BetInfo();
			preBetInfo = new BetInfo();
			this.hitArr = [Sd00, Sd01, Sd03, Sd04, SdOd, SdEv];
			this.rateArr = [1,1,1,1,1,1];
		}
		override public function initBet(minBet:Number, maxBet:Number, currentBalance:Number, initChipValueArr:Array):void 
		{
			super.initBet(minBet, maxBet, currentBalance, initChipValueArr);
			//var newBetArr:Array = super.initBetPlace(super.hitArr);	
			var newBetArr:Array = super.initBetPlace(super.hitArr);
			
			Sd00Bet = newBetArr[0], Sd01Bet = newBetArr[1], Sd03Bet = newBetArr[2], Sd04Bet = newBetArr[3], SdOddBet = newBetArr[4], SdEvenBet = newBetArr[5];
			
			super.betArr = [Sd00Bet, Sd01Bet, Sd03Bet, Sd04Bet, SdOddBet, SdEvenBet];
		}
		
			
		override public function confirmBtnClickHandler(e:MouseEvent):void {
			dispatchEvent(new EventData(EventData.CONFIRM_BETTING, true));		
		}
		
		public function saveBetting():void {
			for (var i:int = 0; i < this.betInfo.arrBettingValue.length; i++ ) {
				
				this.betInfo.arrBettingValue[i] = BetPlace(this.betArr[i]).getBetValue();			
				trace("saveBetting i" + i + ":"+ this.betInfo.arrBettingValue[i])
			}
		}
		
		override public function clearBtnClickHandler(e:MouseEvent):void {
			var tmpBetting:Number = 0;
			tmpBetting = this.getTotalBetValue() - this.saveBetInfo.getTotalBet();			
			if (tmpBetting > 0) {						
				currentBalance = Utility.fixNumber(currentBalance + tmpBetting);
				var event:EventData = new EventData(EventData.CLEAR_BETTING, true);				
				event.newBalance = currentBalance;	
				dispatchEvent(event);				
			}		
		}
		
		
		override public function rebetBtnClickHandler(e:MouseEvent):void {		
			if (this.preBetInfo.getTotalBet() > 0) {	
				//set currentBalance clear before rebet
				var tmpBetting:Number = 0;
				tmpBetting = this.getTotalBetValue() - this.saveBetInfo.getTotalBet();
				currentBalance = Utility.fixNumber(currentBalance + tmpBetting);
				//set currentBalance after rebet
				currentBalance = Utility.fixNumber(currentBalance - this.preBetInfo.getTotalBet());
				var event:EventData = new EventData(EventData.REBET_BETTING, true);
				event.newBalance = currentBalance;	
				dispatchEvent(event);				
			}			
		}
		
		
		override public function updateBet(info:Object):void {
			this.betInfo = BetInfo(info);	
			for (var i:int = 0; i < this.betInfo.arrBettingCode.length; i++) {
				BetPlace(this.betArr[i]).setBetValue(this.betInfo.arrBettingValue[i]);
			}
		}
		
		override public  function betPlaceMouseDownHandler(e:MouseEvent):void {
			if (this.isLock) {
				return;
			}
			
			
			super.showBetSuccess(false, false);
			var activeChip:Chip = chipController.activeChip;
			var event:EventData;
			// Check minBet and maxBet
			if (Utility.fixNumber(this.getTotalBetValue() + activeChip.value) <= this.maxBet) {
				// Check Balance
				var oddValue:Number = checkOdd(bet);
				if (Utility.fixNumber(this.currentBalance - activeChip.value) >= 0) {
					
					// Place Bet
					var betHit:DisplayObject = DisplayObject(e.currentTarget);
					var bet:BetPlace = BetPlace(betHit.parent);
					bet.addBet(activeChip);					
					this.currentBalance = Utility.fixNumber(this.currentBalance - activeChip.value)
					
					var _val = bet.getBetValue();		
					var _max = Number(this.maxBet / oddValue);
					tooltipOver(true, _val, _max, bet.x, bet.y);
					//tooltipOver(true, _val, bet.x, bet.y);
					event = new EventData(EventData.BET_CHANGE);
					event.newBalance = this.currentBalance;
					dispatchEvent(event);
				} else {
					event = new EventData(EventData.SHOW_MS, true);
					event.ms = "OverCredit";
					dispatchEvent(event);
				}
			}else {
				//Utility.showMessage("MAX BETTING");
				event = new EventData(EventData.SHOW_MS, true);
				event.ms = "OverUserUpperLimit";
				dispatchEvent(event);
			}
			
		}
		private function checkOdd(bet:BetPlace):Number {
			var oddValue:Number = -1;
			for (var i = 0 ; i < this.betArr.length; i++) {				
				if (bet == this.betArr[i]) {
					oddValue = Number(this.rateArr[i]);
					return oddValue;
				}				
			}
			return oddValue;
		}
	}
}